import { createRouter, createWebHistory } from 'vue-router'
import {useUserStore} from '@/stores'
// import HomeView from '../views/HomeView.vue'

//createRouter创建路由实例
// createWebHistory history模式 地址不带#号
//createWebHashHistory hash 模式 地址带#号



const router = createRouter({
	//vite 中的环境变量 import.meta.env.BASE_URL 就是vite.config.js中的base配置项
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/LoginPage.vue')
    },
	{
		path: '/',
		component: () => import('@/views/layout/LayoutContainer.vue'),
		redirect: '/article/channel',
		children: [
		        {
		          path: '/article/manage',
		          component: () => import('@/views/article/ArticleManage.vue')
		        },
		        {
		          path: '/article/channel',
		          component: () => import('@/views/article/ArticleChannel.vue')
		        },
		        {
		          path: '/user/profile',
		          component: () => import('@/views/user/UserProfile.vue')
		        },
		        {
		          path: '/user/avatar',
		          component: () => import('@/views/user/UserAvatar.vue')
		        },
		        {
		          path: '/user/password',
		          component: () => import('@/views/user/UserPassword.vue')
		        }
		      ]
	}
  ]
})

//登录访问拦截 => 默认直接放行的
//根据返回值决定，是放行还是拦截
// 返回值:
//1. undefined / true 直接方向
//2. false拦回from的地址页面
//3. 具体路径 或者 路径对象 拦截到对应的地址
 router.beforeEach(async (to, from) => {
	 //如果没有token，且访问的是非登录页，拦截到登录页。
	 const userStore = useUserStore()
	if(!userStore.token&&to.path != '/login') return '/login'
 }) 



export default router
